<?php

$name = $_POST['name'];
$server = $_POST['server'];
//$name = $_GET['name'];
//$server = $_GET['server'];

$stringToWrite = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<ffxivcalc>\n<charinfo>\n";

$url = "http://lodestone.finalfantasyxiv.com/rc/search/search?tgt=77&q=".$name."&cms=&cw=".$server;

//$url = "http://lodestone.finalfantasyxiv.com/rc/search/search?tgt=77&q=Nemi Chan&cms=&cw=5";

$options = array( 'http' => array(
		'user_agent'    => 'crawler',                                // Tell Lodestone we are an outside website.
		'max_redirects' => 10,        							    // Set maximum redirects allowed.
		'timeout'       => 120,										// Set a timeout limit just in case.
		'header'        => 'Accept-language: en-us,en;q=0.5',       // Tell Lodestone to output English instead of Japanese.
	) );															// Header can be set for English/French/German/Japanese.
$context = stream_context_create( $options );
	
$input = file_get_contents($url, false, $context) or die("Could not access file: ".$url);

//echo $input;
$linkStart = strpos ($input, "cicuid=");
$linkEnd = strpos($input, '"', $linkStart+7);

$cuid = substr($input ,$linkStart+7, $linkEnd-($linkStart+7));

//echo $cuid;

$charUrl = "http://lodestone.finalfantasyxiv.com/rc/character/status?cicuid=$cuid";

//$charUrl = "http://lodestone.finalfantasyxiv.com/rc/character/status?cicuid=9545338";

$charInput = file_get_contents($charUrl, false, $context);
$char = findString("profile-plate0-header", null, "<div class=\"profile-plate-header-inner\">", "</div>", $charInput);
$serverStart = strpos ($char, '(');
$serverEnd = strpos ($char, ')');
$charName =  substr($char, 0, $serverStart);
$serverName =  substr($char, $serverStart+1, $serverEnd-($serverStart+1));
$stringToWrite .= "<charName>".trim($charName)."</charName>\n";
$stringToWrite .= "<charServer>".trim($serverName)."</charServer>\n";
//echo $charInput;

$main = findString("profile-table", "skill", "<td colspan=\"2\">", "</td>", $charInput);
$stringToWrite .= "<mainJob>".trim($main)."</mainJob>\n";
$pgl = findString("id=\"pgl\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<pgl>".$pgl."</pgl>\n";
$gla = findString("id=\"gla\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<gla>".$gla."</gla>\n";
$mrd = findString("id=\"mrd\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<mrd>".$mrd."</mrd>\n";
$arc = findString("id=\"arc\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<arc>".$arc."</arc>\n";
$lnc = findString("id=\"lnc\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<lnc>".$lnc."</lnc>\n";
$snt = findString("id=\"snt\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<snt>".$snt."</snt>\n";
$thm = findString("id=\"thm\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<thm>".$thm."</thm>\n";
$con = findString("id=\"con\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<con>".$con."</con>\n";
$crp = findString("id=\"crp\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<crp>".$crp."</crp>\n";
$bsm = findString("id=\"bsm\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<bsm>".$bsm."</bsm>\n";
$arm = findString("id=\"arm\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<arm>".$arm."</arm>\n";
$gld = findString("id=\"gld\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<gld>".$gld."</gld>\n";
$tan = findString("id=\"tan\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<tan>".$tan."</tan>\n";
$wvr = findString("id=\"wvr\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<wvr>".$wvr."</wvr>\n";
$alc = findString("id=\"alc\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<alc>".$alc."</alc>\n";
$cul = findString("id=\"cul\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<cul>".$cul."</cul>\n";
$min = findString("id=\"min\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<min>".$min."</min>\n";
$btn = findString("id=\"btn\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<btn>".$btn."</btn>\n";
$fsh = findString("id=\"fsh\"", "class=\"contents-table1TD", "<b>", "</b>", $charInput);
$stringToWrite .= "<fsh>".$fsh."</fsh>\n";


		/*
		// Get details of ability from yg with the id from zam
		$charUrl = "http://ffxiv.zam.com".$match[2];
		$charInput = file_get_contents($charUrl);
		
		$abilityDescription = findString("<div class=\"xivtt xivtt-abil\">", NULL, "<div class=\"xivtt-inner\">" ,"<div class=\"tabwrapper\">", $abilityInput);
		
		$head = getPart("<div class=\"xivtt-head\">", $abilityDescription);
		
		//echo $head;
		
		$name = findString(NULL, NULL, "<h2>", "</h2>", $head);
		$stringToWrite .= "<name>".$name."</name>";
		
		$cost = findString(NULL, NULL, "</span>", "</div>", $head);
	
		$stringToWrite .= "<actionCost>".getNumber($cost)."</actionCost>";
		
		$body =  getPart("<div class=\"xivtt-body\">", $abilityDescription);
		
		$rank = findString("Optimal Rank", NULL, "</span>", "<br>", $body);
		
		$stringToWrite .= "<rank>".getNumber($rank)."</rank>";
		
		$mp = findString("MP Cost", NULL, "</span>", "<br>", $body);
		
		$stringToWrite .= "<mp>".getNumber($mp)."</mp>";
		
		$tp = findString("TP Cost", NULL, "</span>", "<br>", $body);
		
		$stringToWrite .= "<tp>".getNumber($tp)."</tp>";
		
		$text = findString(NULL, NULL, "<div style=\"margin:5px 0 0\">", "</div>", $body);
		
		$stringToWrite .= "<text>".$text."</text>";
		
		//For each ability get the icon from yg.com
		$skillUrl = "http://ffxiv.yg.com/api/lookup?s=".urlencode($name)."&o=skill";
		$skillJSON = file_get_contents($skillUrl);
		$icon = findString($name, NULL, "\"medium\":\"", "\",\"", $skillJSON);
		$stringToWrite .= "<mediumIcon>".str_replace("\\","",$icon)."</mediumIcon>";  
		$icon = findString($name, NULL, "\"large\":\"", "\"}", $skillJSON);
		$stringToWrite .= "<largeIcon>".str_replace("\\","",$icon)."</largeIcon>";  
		*/
 
$stringToWrite .= "</charinfo>\n</ffxivcalc>\n";
echo $stringToWrite;


function getPart($anchor, $string){

	$start = strpos ($string, $anchor);
	$firstEnder = strpos ($string, "</div>" , $start);
	$secondEnder = strpos ($string, "</div>" , $firstEnder+6);
	Return substr ($string, $start+strlen($anchor), $secondEnder-($start+strlen($anchor))); 

}
function getNumber($text){
	if ($text != ""){
		if(preg_match("/(\d+)/", $text, $match)){
			return $match[0];
		}else{
			return "";
		}
	}else{
		return "";
	}
}

function findString($parentAnchor, $childAnchor, $startT, $endT, $file){
		$abort = false;
		$varDefStart = 1;
		if (!is_null($parentAnchor)){
			$varDefStart = strpos ($file, $parentAnchor);
			if ($varDefStart== False){
				$abort = true;
			}
		}
		if (!is_null($childAnchor)){
			$varDefStart = strpos ($file, $childAnchor, $varDefStart);
			if ($varDefStart== False){
				$abort = true;
			}
		}
		//if ($varDefStart >= 0 && $varDefStart<=strlen($file) && !($varDefStart==False)) {
			//echo "first anchor:".$varDefStart."<br>";
		$startPointRaw  = strpos  ($file, $startT, $varDefStart);
		if (($startPointRaw!= False) && ($abort == false)){
			$startPoint = $startPointRaw+strlen($startT);
			//echo "startpoint:".$startPoint."<br>";
			$endPoint = strpos  ($file, $endT, $startPoint);
			//echo "endpoint:".$endPoint."<br>";
			Return substr ($file, $startPoint, ($endPoint-($startPoint)));
		}else{
			Return "";
		}

}

?>